package net.zabuchy.issue.dao;
 
import java.util.List;

import net.zabuchy.IssueHelper.IssueStatuses;
import net.zabuchy.issue.action.IssueAction;
import net.zabuchy.issue.entity.Attachment;
import net.zabuchy.issue.entity.Issue;
import net.zabuchy.issue.entity.IssueLink;
import net.zabuchy.issue.entity.Notification;

import org.apache.log4j.Logger;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

public class IssueDaoImpl extends HibernateDaoSupport implements IssueDao {
 
	private static final Logger LOGGER = Logger.getLogger(IssueAction.class.getName());
	
    public void add(Issue issue) {
    	LOGGER.debug("adding the following issue: " + issue.getTitle());
        getHibernateTemplate().save(issue);
    }
 
    public void addAttachment(Attachment attach) {
    	LOGGER.debug("adding the following attach: " + attach.getFile());
    	getHibernateTemplate().save(attach);
    }
    
    public void addLink(IssueLink link) {
    	LOGGER.debug("adding the following attach: " + link.getComment());
    	getHibernateTemplate().save(link);
    }
    
    public void addNotification(Notification notification) {
    	LOGGER.debug("adding the following notification: " + notification.getUserId());
    	getHibernateTemplate().save(notification);
    }
    
    public void delete(Issue issue) {
        getHibernateTemplate().delete(issue);
    }
 
    public void deleteNotification(Notification noti) {
        getHibernateTemplate().delete(noti);
    }
    
    public void deleteAttachment(Attachment attach) {
        getHibernateTemplate().delete(attach);
    }
    
    public void deleteLink(IssueLink link) {
        getHibernateTemplate().delete(link);
    }
    
	public List<Issue> findAll() {
        return getHibernateTemplate().find("from Issue");
    }
 
    public List<Issue> findAllForProject(int projectId) {
    	return getHibernateTemplate().find("from Issue where PROJECT_ID=?", projectId);
    }
    
    public List<Issue> findAllForProjectAndIteration(int projectId, int iterationId) {
    	return getHibernateTemplate().find("from Issue where PROJECT_ID=? and ITERATION_ID=?", projectId, iterationId);
    }
    
    public List<Issue> findAllForProjectAndIterationAndStatus(int projectId, int iterationId, IssueStatuses status) {
    	return getHibernateTemplate().find("from Issue where PROJECT_ID=? and ITERATION_ID=? and STATUS=?", projectId, iterationId, status.toString());
    }
    
    public List<Issue> findAllForProjectToConfirmation(int projectId, int iterationId) {
    	return getHibernateTemplate().find("from Issue where PROJECT_ID=? and ITERATION_ID=? and CONFIRMED=?", projectId, iterationId, false);
    }
    
    public Issue findById(int id) {
        return (Issue) getHibernateTemplate().find("from Issue where ISSUE_ID=?", id).get(0);
    }
    
    public void dupa() {
    	
    	getHibernateTemplate().getSessionFactory().getCurrentSession().createCriteria(IssueDaoImpl.class);
    	
    }
 
    public void update(Issue issue) {
        getHibernateTemplate().update(issue);
    }
    
    public List<Issue> findIssuesRelatedToIteration(Integer id) {
    	return getHibernateTemplate().find("from Issue where ITERATION_ID=?", id);
    }

}